﻿ 
var InitPostUrl = "../Handler/CommonHandler.ashx";
var regEx = {
    user_name: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,5}){1,2})$/,
    auth_code: /^[a-zA-Z0-9_-]$/,
    Lincense: /^[0-9]+$/,
    real_name: /^[\u4e00-\u9fa5]+$/,
    CompAddr: /^[\u4e00-\u9fa5]+$/,
    id_card_num: /(^[0-9]{17}[0-9xX]$)|(^[0-9]{15}$)/,
    email: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,5}){1,2})$/,
    Mobile: /^1[3|4|5|8][0-9]\d{4,8}$/,
    password: new RegExp("^[\\w\\.(!@#$%&)]+$")
};
var YXQ = {
    
    Main: {},
    Common: {}
}

YXQ.Common = {
    Message: function (msg) {
        $.messager.alert("温馨提示", msg);
        return;
    },
    SelAll: function () {
        $("input:checkbox").each(function (i) {
            this.checked = true;

        });

    },
    NoSelAll: function () {
        $("input:checkbox").each(function (i) {
            this.checked = false;
        });
    },
    GetCheckboxItem: function () {
        var allSel = "";
        $("input:checkbox").each(function (i) {
            if (this.checked) {
                if(this.value!="on")
                allSel += this.value + ",";
            }
        });
        return allSel.substring(0, allSel.length - 1);

    },
    GetOneItem: function () {
        var allSel = "";
        $("input:checkbox").each(function (i) {
            if (allSel == "") {
                allSel = this.value;
            }
        });

    }
}


YXQ.Main = {
    ChkUser: function (val) {
        if (val == "") {
            $("#tlpUser").html("对不起，请输入用户名！");
            return;
        }
       
        if (! /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(val)) {
            
            return;
        }
      
        YXQCore.Form.PostData( InitPostUrl,
        { OpType: "ChkUser", PostData: val },
        function (data) {
            
            if (data.ResultMessage == "Success") {
                $("#tlpUser").html("<font color='red'>对不起，用户名已经被其他用户占用！</font>");
            } else {
                $("#tlpUser").html("<font color='green'>恭喜你用户名可以使用！</font>");
              
            }
        });
    },
    ChkCode: function (val) {
        if (val == "") {
            $("#tlpCode").html("对不起，请输入验证码！");
            return;
        }
        YXQCore.Form.PostData(InitPostUrl,
       { OpType: "ChkCode", PostData: val },
       function (data) {
          
           if (data.ResultMessage == true) {
               $("#tlpCode").html("<font color='green'>输入正确！</font>");
           } else {
             
               $("#tlpCode").html("<font color='red'>输入错误！</font>");

           }
       });
    },
    UserLogin: function () {
        var uname = $("[datafield='uname']").val();
        if (uname == "") {
            YXQ.Common.Message("对不起，请输入用户名！");
            return false;
        }
        var upwd = $("[datafield='upwd']").val();
        if (upwd == "") {
            YXQ.Common.Message("对不起，请输入密码！");
            return false;
        }
    },
    GetBadge: function (cid) {
       
        YXQCore.Form.PostData(InitPostUrl,
                { OpType: "GetBadge", PostData: cid },
            function (data) { 
                var tbDetail = $("#tbDetail");
                if (data.ResultMessage != null) {
                    $("[DataField='spPic']").attr("src", data.ResultMessage.UserLogo);
                }
                YXQCore.Form.SetDivFormData(tbDetail,data);
            });
    },
    SaveBadge: function () {
       
        var tbDetail = $("#tbDetail"); 
        var jsonData = YXQCore.Form.GetDivFormData(tbDetail);
        var jsonDataString = $.toJSON(jsonData);
        YXQCore.Form.PostData(InitPostUrl,  
                { OpType: "SaveBadge", PostData: jsonDataString },
                function (data) {
                    
                    if (data == "Success") {
                        YXQCore.Form.ClearDivData();
                        YXQ.Common.Message("恭喜你数据保存成功！");

                    } else {
                        YXQ.Common.Message("对不起数据保存失败！");
                    }
            });
    },
    DelBadge: function () {
        var selItem = YXQ.Common.GetCheckboxItem();
        YXQCore.Form.PostData(InitPostUrl,
                { OpType: "DelBadge", PostData: selItem },
                function (data) {

                    if (data.ResultMessage == "Success") {
                       
                        YXQ.Common.Message("恭喜你操作成功！");
                        window.location.reload();

                    } else {
                        YXQ.Common.Message("对不起操作失败！请联系管理员");
                    }
                });
    },
    OpenDialog: function (url, tt,w,h) { 
    $("#openwin").dialog({
        title: tt,
        href: url,
        width: w,
        height: h, 
            
    });
        
    $("#openwin").dialog("open");
       
    },
    CloseDialog: function () {
        $("#openwin").dialog("close");
    },
    DelMessage: function (curId,curuid) {
        YXQCore.Form.PostData(InitPostUrl, {OpType:"DelMessage",PostData:curId,StrValue:curuid},
            function (data) {
                if (data.ResultMessage == "Success") {
                    YXQ.Common.Message("恭喜你操作已成功！");
                    window.location.reload();
                } else {
                    YXQ.Common.Message("对不起，操作失败，请联系管理员");
                }
            }
            );
    },
    
    UpdateEmail: function () {
        
        var email = $("#txtEmail").val();
        if (email == "") {
            YXQ.Common.Message("您没有填写邮箱，不能更新数据！");
            return;
        }
      
        if (!regEx.email.test(email)) {
            YXQ.Common.Message("请填写正确的邮箱格式，如example@site.com！");
            return;
        }

        YXQCore.Form.PostData(InitPostUrl, { OpType: "UpdateEmail", PostData: email },
            function (data) {
                if (data.ResultMessage == "Success") {
                    $("#spcontent").html(email);
                    $("#spemail").show();
                    $("#spemailinput").hide();
                    YXQ.Common.Message("数据更新成功！");
                    
                } else {
                    YXQ.Common.Message("数据更新失败，请联系网站管理员！");
                    return;

                }
            });

    },
    UpdatePhone: function () {
        var phone = $("#txtPhone").val();
        if (phone == "") {
            YXQ.Common.Message("您没有填写电话号码，不能更新数据！");
            return;
        }

        if (!regEx.Mobile.test(phone)) {
            YXQ.Common.Message("请填写正确的手机格式，如133/135/189等！");
            return;
        }

        YXQCore.Form.PostData(InitPostUrl, { OpType: "UpdatePhone", PostData: phone },
            function (data) {
                if (data.ResultMessage == "Success") {
                    $("#sphcontent").html(email);
                    $("#spphone").show();
                    $("#spphoneinput").hide();
                    YXQ.Common.Message("数据更新成功！");

                } else {
                    YXQ.Common.Message("数据更新失败，请联系网站管理员！");
                    return;

                }
            });
    },

    ChangeArea: function (val) {
        YXQCore.Form.PostData(InitPostUrl, { OpType: "GetSonArea", PostData: val },

            function (data) {
                
                if (data.ResultMessage != "") {
                    var objs = $.evalJSON(data.ResultMessage);
                    var option = "<option value='-1'>请选择</option>";
                    $(objs).each(function (a) {
                       
                        option += "<option value='"+this.Id+"'>"+this.AreaName+"</option>";
                    });
                    $("[datafield='areaid']").html(option);
                }
            });
    }


};